package de.gwtprime.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.ApplyTabHeightEvent;
import de.gwtprime.client.event.ArticleSectionOnTypeEvent;
import de.gwtprime.client.event.handler.ApplyTabHeightEventHandler;
import de.gwtprime.client.event.handler.ArticleSectionOnTypeEventHandler;
import de.gwtprime.client.widgets.javascript.CustomTabsComposite;
import de.gwtprime.shared.VoteType;
import de.gwtprime.shared.requestFactory.ArticleProxy;
import de.gwtprime.shared.requestFactory.ArticleSectionProxy;

public class ArticleSectionWidget extends Composite {

	ArticleSectionWidgetUiBinder uiBinder = GWT
			.create(ArticleSectionWidgetUiBinder.class);

	interface ArticleSectionWidgetUiBinder extends
			UiBinder<Widget, ArticleSectionWidget> {
	}

	
	@UiField Label titleLabel;
	
	@UiField SimplePanel tabPanel;
	
	/**
	 * Keep the vote state on the UI
	 */
	VoteType voteType;
	
	ClientFactory clientFactory;
	
	final ArticleProxy article;

	public ArticleSectionWidget(final ArticleSectionProxy articleSection, final ArticleProxy article,
			final ClientFactory clientFactory) {
		
		initWidget(uiBinder.createAndBindUi(this));
		
		this.clientFactory = clientFactory;
		this.article = article;
		
		createTabLayout(articleSection);
		titleLabel.setText(articleSection.getTitle());

		onEditHandler(articleSection, clientFactory);
		
	}

	CustomTabsComposite tabWidget;
	private void createTabLayout(ArticleSectionProxy articleSection) {
		
		tabWidget = new CustomTabsComposite();
        tabWidget.add(new ArticleSectionContentWidget(article, articleSection, clientFactory), "Content");
        tabWidget.add(new QuestionTabWidget(clientFactory, articleSection), "Questions");
        tabWidget.add(new HTML("Vorschläge hier"), "Vorschläge");
        
	    tabPanel.add(tabWidget);
	    
	    clientFactory.getEventBus().addHandler(ApplyTabHeightEvent.TYPE, new ApplyTabHeightEventHandler() {

			@Override
			public void applyTabHeight(ApplyTabHeightEvent event) {
				tabWidget.applyCorrectHeight();
				
			}
			
		});
	    
	}

	
	/**
	 * Handles keystrokes (updates the content on the UI) if the article section is 
	 * edited in the article section edit widget.
	 * @param articleSection
	 * @param clientFactory
	 */
	private void onEditHandler(final ArticleSectionProxy articleSection,
			final ClientFactory clientFactory) {
		clientFactory.getEventBus().addHandler(ArticleSectionOnTypeEvent.TYPE, new ArticleSectionOnTypeEventHandler() {

			@Override
			public void onType(ArticleSectionOnTypeEvent event) {
				if(articleSection != null && articleSection.getId() == event.getId()){
					titleLabel.setText(event.getTitle());
				}
			}
		});
	}

}
